package com.demo2do.fms.admin.entity.search.supplier;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.fms.entity.supplier.common.AuditStatus;
import com.demo2do.fms.entity.supplier.common.Type;

import java.util.Map;

/**
 * @description:
 * @author: 11200
 * @date: 2019/7/22
 */
public class CommonStorageSupplierSearch implements QueryBuilder {

    private QueryFilter queryFilter;

    private QueryOrder queryOrder;

    private String name;

    private String code;

    private String branchId;

    private AuditStatus status;

    /**
     * get prepared filter
     *
     * @param refresh whether to re-create filter
     * @return
     */
    public QueryFilter getPreparedFilter(boolean refresh) {

        if (queryFilter == null || refresh){

            queryFilter = new QueryFilter();

            queryFilter.addEquals("type", Type.COMMON_STORAGE);

            queryFilter.addLike("name", name);

            queryFilter.addEquals("code", code);

            queryFilter.addEquals("branch.id", branchId);

            queryFilter.addEquals("status", status);

        }
        return queryFilter;
    }

    /**
     * get order
     *
     * @return
     */
    public QueryOrder getOrder() {

        if (queryOrder == null){
            queryOrder = new QueryOrder("lastModifyTime", "DESC");
        }
        return queryOrder;
    }

    /**
     * build query string template (sql or hql with :keys)
     *
     * @return
     */
    public String buildSentenceTemplate() {
        return "FROM Supplier WHERE disabled = false " + getPreparedFilter(false).clauseSentence() + getOrder().sentence();
    }

    /**
     * build query string template corresponding parameters
     *
     * @return
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getBranchId() {
        return branchId;
    }

    public void setBranchId(String branchId) {
        this.branchId = branchId;
    }

    public AuditStatus getStatus() {
        return status;
    }

    public void setStatus(AuditStatus status) {
        this.status = status;
    }
}
